<?php
namespace Crm\Controller;

use PHPExcel;
use PHPExcel_IOFactory;
use Zend\View\Model\ViewModel;
use Crm\Common\CommonHelper;

/**
 * 客户管理
 */
class CustomerController extends AbstractController
{

    public function listAction()
    {
        $organizationList = array();
        $organizationList = $this->getOrganizationTable()->findAll();
        return new ViewModel(array(
            'organization'=>$organizationList,
        ));
    }

    public function ajaxCustomerListAction()
    {
        $request = $this->request->getPost()->toArray();
        
        $user_name = isset($request['user_name']) ? $request['user_name'] : '';
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        $org_id = isset($request['org_id']) ? $request['org_id'] : '';
        $user_uid = isset($request['user_uid']) ? $request['user_uid'] : '';
        $query = array(
            'user_name' => $user_name,
            'page' => $page,
            'org_id' => $org_id,
            'user_uid' => $user_uid,
        );
        
        $paginator = $this->getUserTable()->find($query);
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        return $viewModel;
    }

    public function addBalanceAction()
    {
        $request = $this->getRequest();
        $user_id = $request->getPost('user_id');
        $add_balance = $request->getPost('add_balance');
        try{
            $this->getUserTable()->beginTransaction();
            $userInfo = $this->getUserTable()->findOne($user_id);
            $this->getUserTable()->updateBalance($user_id,$add_balance + $userInfo['balance']);
            $this->getUserBalanceLogTable()->create(array(
                'user_id'=>$user_id,
                'ubl_income'=>$add_balance,
                'ubl_balance'=>$add_balance + $userInfo['balance'],
                'ubl_remark'=>'后台充值',
                'ubl_detail'=>'后台充值',
            ));

            $this->getUserTable()->commit();
            $response=array(
                'result' => 'true',
                'reason' => '充值成功！',
            );
            die(json_encode($response));
        }catch (\Exception $e){
            $this->getUserTable()->rollBack();
            $response=array(
                'result' => 'false',
                'reason' => '系统异常,处理失败！',
            );
            die(json_encode($response));
        }
        
    }
    public function batchAddBalanceAction()
    {
        $request = $this->request->getPost()->toArray();
        $user_name = isset($request['user_name']) ? $request['user_name'] : '';
        $org_id = isset($request['org_id']) ? $request['org_id'] : '';
        $user_uid = isset($request['user_uid']) ? $request['user_uid'] : '';
        $add_balance = isset($request['add_balance']) ? $request['add_balance'] : '';
        try{
            $this->getUserTable()->beginTransaction();
            $query = array(
                'user_name' => $user_name,
                'org_id' => $org_id,
                'user_uid' => $user_uid,
            );
            
            $userList = $this->getUserTable()->findAll($query);
            foreach ($userList as $user){
                $this->getUserTable()->updateBalance($user['user_id'],$add_balance + $user['balance']);
                $this->getUserBalanceLogTable()->create(array(
                    'user_id'=>$user['user_id'],
                    'ubl_income'=>$add_balance,
                    'ubl_balance'=>$add_balance + $user['balance'],
                    'ubl_remark'=>'后台批量充值',
                    'ubl_detail'=>'后台批量充值',
                ));
            }
            
            $this->getUserTable()->commit();
            $response=array(
                'result' => 'true',
                'reason' => '批量充值成功！',
            );
            die(json_encode($response));
        }catch (\Exception $e){
            $this->getUserTable()->rollBack();
            $response=array(
                'result' => 'false',
                'reason' => '系统异常,处理失败！',
            );
            die(json_encode($response));
        }
        
    }
    public function batchCleanBalanceAction()
    {
        $request = $this->request->getPost()->toArray();
        $user_name = isset($request['user_name']) ? $request['user_name'] : '';
        $org_id = isset($request['org_id']) ? $request['org_id'] : '';
        $user_uid = isset($request['user_uid']) ? $request['user_uid'] : '';
        try{
            $this->getUserTable()->beginTransaction();
            $query = array(
                'user_name' => $user_name,
                'org_id' => $org_id,
                'user_uid' => $user_uid,
            );
            
            $userList = $this->getUserTable()->findAll($query);
            foreach ($userList as $user){
                $this->getUserTable()->updateBalance($user['user_id'],0);
                $this->getUserBalanceLogTable()->create(array(
                    'user_id'=>$user['user_id'],
                    'ubl_outcome'=>$user['balance'],
                    'ubl_balance'=>0,
                    'ubl_remark'=>'后台批量清零',
                    'ubl_detail'=>'后台批量清零',
                ));
            }
            
            $this->getUserTable()->commit();
            $response=array(
                'result' => 'true',
                'reason' => '批量清零成功！',
            );
            die(json_encode($response));
        }catch (\Exception $e){
            $this->getUserTable()->rollBack();
            $response=array(
                'result' => 'false',
                'reason' => '系统异常,处理失败！',
            );
            die(json_encode($response));
        }
        
    }

    
    public function feedbackAction()
    {
        return new ViewModel();
    }
    
    public function ajaxFeedbackListAction()
    {
        $request = $this->request->getPost()->toArray();
    
        (int) $user_id = isset($request['user_id']) ? $request['user_id'] : '';
        (int) $user_name = isset($request['user_name']) ? $request['user_name'] : '';
        (int) $start_time = isset($request['start_time']) ? $request['start_time'] : '';
        (int) $end_time = isset($request['end_time']) ? $request['end_time'] : '';
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        $query = array(
            'user_id' => $user_id,
            'user_name' => $user_name,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'page' => $page
        );
    
        $paginator = $this->getFeedbackTable()->getFeedbackList($query);
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        return $viewModel;
    }
    public function excelFeedbackAction()
    {
        $outExcelList = array();
        $outExcelList = $this->getFeedbackTable()->getOutExcelList();
    
        $objPHPExcel = new PHPExcel();
        //设置title
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', '留言ID')
        ->setCellValue('B1', '用户ID')
        ->setCellValue('C1', '用户名称')
        ->setCellValue('D1', '留言时间')
        ->setCellValue('E1', '联系电话')
        ->setCellValue('F1', '留言内容');
    
        //详细数据
        foreach ($outExcelList as $key=>$feedback){
            $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.($key + 2), $feedback['id'])
            ->setCellValue('B'.($key + 2), $feedback['user_id'])
            ->setCellValue('C'.($key + 2), $feedback['user_name'])
            ->setCellValue('D'.($key + 2), $feedback['create_time'])
            ->setCellValue('E'.($key + 2), $feedback['cellphone'])
            ->setCellValue('F'.($key + 2), $feedback['message']);
        }
    
        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle('用户反馈留言');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); // 生成excel文件 Excel2007
        CommonHelper::browserExport('Excel5', 'feedback_'.date('YmdHis').'.xls'); // 输出到浏览器
        $objWriter->save("php://output");
        exit();
    }
}